/*
  +++++++++++++++++++++++++++++++++++++++++++
  +  Description: 代码生成业务表
  +  Created by 王大宸 on 2023-09-23 21:25:55
  +  Created with IntelliJ IDEA.
  ++++++++++++++++++++++++++++++++++++++++++++
 */
const path = HTTP_BIZ_URI + "/dev/table/info";
layui.use(['table', 'form'], function () {
    let table = layui.table;
    let form = layui.form;

    /* 初始化表格 */
    $.table.init(table, options())

    /* 操作栏监听 */
    tool(table);


    /* 查询按钮 */
    form.on('submit(searchDevTableInfo)', function (data) {
        reloadTableData(table);
        return false; // 阻止默认 form 跳转
    });

    /* 导入表 */
    form.on('submit(importDevTableInfo)', function (data) {
        let uri = path + '/db_list';
        //  location.href = path + '/db_list'
        $.model.layerHtmlFull('导入表 - 选择需要生成代码的数据库表', uri);

        return false; // 阻止默认 form 跳转
    })

    /* 下载代码 */
    form.on('submit(downloadDevTableInfo)', function (data) {

        let checkStatus = table.checkStatus('layTable');
        let checkData = checkStatus.data;
        if (0 === checkData.length) {
            layer.alert("请选择需要的表", {
                time: 5000,
                btn: ['确定'],
                icon: 0
            });
            return false;
        }
        let tableNames = [];
        for (let i = 0; i < checkData.length; i++) {
            tableNames.push(checkData[i].id);
        }

        console.log(tableNames)
        location.href = path + "/code?id=" + tableNames.join();

        // location.href = path + '/code'
        return false; // 阻止默认 form 跳转
    })

    /* 新增表 */
    form.on('submit(addDevTableInfo)', function (data) {
        location.href = path + '/add'
        return false; // 阻止默认 form 跳转
    })


});

/**
 * 数据表格查询条件(必须有,不然表格重载不了)
 */
function queryWhere() {
    let tableName = $("#tableName").val();
    let funcName = $("#funcName").val();
    return {
        tableName: tableName,
        funcName: funcName,
    }
}

/**
 * 刷新表格数据
 * @param table
 */
function reloadTableData(table) {
    $.table.reloadData(table, options());
}

/**
 * 表格操作
 * @param table
 */
function tool(table) {
    table.on('tool(layFilter)', function (obj) {
        let data = obj.data;
        if ('delete' === obj.event) {
            $.table.delete({
                url: path + '/' + data.id,
                title: data.name
            })
        }
        if ('update' === obj.event) {
            let uri = path + '/edit/' + data.id;
          //  $.model.layerFull(uri)
            $.model.layerHtmlFull('编辑', uri);
        }
        if ('preview' === obj.event) {
            let uri = path + '/view/' + data.id;
            $.model.layerHtmlFull('预览代码', uri);
            // let width = ($(window).width() - 20);
            // let height = ($(window).height() - 20);
            // layer.open({
            //     type: 2,
            //     title: '预览代码',
            //     content: $.http.verifyURL(uri),
            //     area: [width + 'px', height + 'px']
            // });
        }
        if ('downloadCode' === obj.event) {
            location.href = path + "/code?id=" + data.id;
        }
        if ('addColumn' === obj.event) {
            location.href = path + '/column/' + data.id;
        }

    })
}

function options() {
    return {
        url: path + "/page",
        where: queryWhere(),
        toolbar: false,
        defaultToolbar: false,
        // even: true,
        // skin: 'grid',
        cols: [[
            {type: 'checkbox'},
            {field: 'tableName', title: '表名称',width: 300, align: "center"},
            {field: 'clazzName', title: '实体类名称', width: 240, align: "center"},
            {field: 'funcName', title: '功能名', width: 200, align: "center"},
            {field: 'askUri', title: '访问地址', align: "center"},
            {field: 'funcAuthor', title: '开发者',width: 180, align: "center"},
            {fixed: 'right', title: '操作', toolbar: '#toolbarHandle', width: 400, align: "center"}
        ]]
    }

}

